!function (global,$) {
    const ListView = function (selector,ops) {
        this.rootSelector = selector;
        this.ops = ops;
        this.loading = false;
        this.pageNo = 1;
        this.sortState = null;
        resetScroll();
        $(global).scroll(this.handleScrollEvent().bind(this));
        this.createSortDom();
        const {onCreate} = this.ops;
        if (onCreate) {
            this.ops.onCreate.bind(this)();
        }
    };
    ListView.prototype.createSortDom = function(){
        const self = this;
        const {sortRender,sortData,onSortChange,sortDefaultKey} = this.ops;
        if(!objIsEmpty(sortRender)&&!objIsEmpty(sortData)){
            const sortItemDomList = sortData.map(function (item) {
                const {key,title,multiple} = item;
                if (!objIsEmpty(multiple)) {
                    return '<li id="sort-'+key+'" class="filter-item" style="cursor:pointer;">'+
                        '<span class="filter-text">'+title+'</span>'+
                        '<span class="filter-flg" style="margin-left:5px;">' +
                        '<i class="anticon icon-up"></i>' +
                        '<i class="anticon icon-down"></i>'+ '</span>'+ '</li>';
                }
                return '<li id="sort-'+key+'" class="filter-item" style="cursor:pointer;">'+
                    '<span class="filter-text">'+title+'</span>'+ '</li>';
            });
            const sortListDom = buildDomList2String(sortItemDomList);
            const sortDom = '<ul class="filter-list">'+sortListDom+'</ul>';
            $(sortRender).wrapInner(sortDom);
            // 初始化默认值
            if (sortDefaultKey) {
                const defaultSortItem = sortData.find(function (item) {
                    return sortDefaultKey === item.key;
                });
                if (defaultSortItem) {
                    const {key,multiple} = defaultSortItem;
                    const domId = '#sort-' + key;
                    $(domId).addClass('active');
                    if (multiple) {
                        const filterFlg = $(domId).children('.filter-flg').children('.anticon');
                        $(filterFlg[1]).addClass('open');
                        this.sortState = {key:key,state:'desc'}
                    }else{
                        this.sortState = {key:key,state:null}
                    }

                }
            }
            // 处理排序项点击事件
            $(global.document.body).on('click','.filter-item', function () {
                const id = $(this).attr('id');

                const keyVal = /sort-(.+)/.exec(id)[1];
                const sortItem = sortData.find(function (item) {
                    return item.key === keyVal;
                });

                const {multiple} = sortItem;
                if (!objIsEmpty(self.sortState)) {
                    const {key} = self.sortState;
                    if (key === keyVal&&objIsEmpty(multiple)) {
                        return;
                    }else if(key !== keyVal&&!objIsEmpty(multiple)){
                        self.sortState = null;
                    }
                    self.unlockLoading();
                    self.resetPageNo();
                    $(self.rootSelector).empty();
                }
                $('.filter-item').removeClass('active');
                const filterFlg = $('.filter-flg').children('.anticon');
                filterFlg.removeClass('open');
                $(this).addClass('active');
                if (!objIsEmpty(multiple)) {
                    // console.log('--',self.sortState);
                     if (objIsEmpty(self.sortState)||objIsEmpty(self.sortState.state)) {
                         const filterFlg = $(this).children('.filter-flg').children('.anticon');
                         $(filterFlg[1]).addClass('open');
                         self.sortState = {key:sortItem.key,state:'desc'}
                     }else if(self.sortState.state === 'desc'){
                         const filterFlg = $(this).children('.filter-flg').children('.anticon');
                         $(filterFlg[0]).addClass('open');
                         self.sortState = {key:sortItem.key,state:'asc'}
                     }else if(self.sortState.state === 'asc'){
                         const filterFlg = $(this).children('.filter-flg').children('.anticon');
                         $(filterFlg[1]).addClass('open');
                         self.sortState = {key:sortItem.key,state:'desc'}
                     }
                }else{
                    self.sortState = {key:sortItem.key,state:null}
                }
                if (!objIsEmpty(onSortChange)) {
                    onSortChange.bind(self)(self.sortState);
                }
            });
        }
    };
    ListView.prototype.load = function (data,call,reset) {
        const domList = data.map(function (item) {
            return this.ops.render.bind(this)(item);
        }.bind(this));
        const htmlText = buildDomList2String(domList);
        if (reset) {
            $(this.rootSelector).wrapInner('<div>'+htmlText+'</div>');
        }else{
            $(this.rootSelector).append('<div>'+htmlText+'</div>');
        }
        if (!call) {
            return;
        }
        call.bind(this)();
    };
    ListView.prototype.reload = function(data){
        for (let i=0;i<data.length;i++){
            const renderDom = this.ops.render(data[i]);
        }
    };
    ListView.prototype.render4Tmpl = function(selector,args){
        let s = $(selector).html();
        const compiled = _.template(s);
        return compiled(args);
    };
    ListView.prototype.handleScrollEvent = function () {
        return function () {
            if (typeof this.ops.onLoadMore === typeof undefined
                ||this.ops.onLoadMore === null) {
                return;
            }
            //可视区域的高度
            const screen = window.screen.height;
            // 可视区域顶部的位置与网页底部的距离
            const compare = document.body.clientHeight-$(global).scrollTop();
            const y = compare - screen - 20;
            if (y<=0&&!this.loading) {
                this.loading = true;
                this.ops.onLoadMore.bind(this)(++this.pageNo);
            }
        }
    };
    ListView.prototype.unlockLoading = function () {
        this.loading = false;
    };
    ListView.prototype.loadingEmpty = function () {
        this.pageNo--;
        this.loading = true;
    };
    ListView.prototype.resetPageNo = function(){
        this.pageNo = 1;
    };
    ListView.prototype.setState = function(obj){
        this.state = Object.assign(this.state||{},obj||{});
    };
    ListView.prototype.showPlaceholder = function () {
        const htmlText = '<div class="list-placeholder">暂无数据...</div>';
        $(this.rootSelector).wrapInner(htmlText);
    };
    function objIsEmpty(obj) {
        return typeof obj === typeof undefined
            || obj === null || obj === false;

    }
    function buildDomList2String(domList) {
        let out = '';
        domList.forEach(function (item) {
            out += item;
        });
        return out;
    }
    function resetScroll() {
        if ('scrollRestoration' in history) {
            history.scrollRestoration = 'manual';
        }else{
            window.onunload= function(){
                window.scrollTo(0,0);
            };
        }
    }
    !('ListView' in global) && (global.ListView = ListView);
}(window, jQuery);